package chinatelecom.feilong.framework.web.manager.service.impl;

import chinatelecom.feilong.framework.web.license.entity.License;
import chinatelecom.feilong.framework.web.manager.entity.VerifyLicenseModel;
import chinatelecom.feilong.framework.web.manager.service.ILicenseManagerService;
import chinatelecom.feilong.license.constant.LicenseConstant;
import chinatelecom.feilong.license.manager.LicenseManager;
import chinatelecom.feilong.license.util.LicenseManagerFactory;
import com.ideal.license.core.exception.CheckParamException;
import com.ideal.license.core.model.BaseComputerModel;
import com.ideal.license.core.model.JiCengLicenseModel;
import com.ideal.license.core.model.LicenseModel;
import com.ideal.license.verifier.manager.VerifyModel;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Properties;

@Service
public class LicenseManagerServiceImpl implements ILicenseManagerService{
    private static final Logger logger = LoggerFactory.getLogger(LicenseManagerServiceImpl.class);

    /**
     * 新建证书.
     * @param license
     * @return
     * @throws Exception
     */
    @Override
    public String createLicense(License license) throws Exception {
        JiCengLicenseModel model = new JiCengLicenseModel();
        this.initProperties(model);
        this.initModel(license, model);

        LicenseManager licenseManager = LicenseManagerFactory.create();
        String path = licenseManager.createLicense(model);

        return path;
    }

    /**
     * 验证证书.
     * @param model
     * @return
     * @throws Exception
     */
    @Override
    public LicenseModel verifyLicense(VerifyLicenseModel model) throws Exception {
        VerifyModel vmodel = new VerifyModel();

        this.initVpProperties(vmodel);
        this.initVmodel(vmodel, model);

        LicenseManager licenseManager = LicenseManagerFactory.create();
        LicenseModel licenseModel = licenseManager.verfy(vmodel);

        return licenseModel;
    }

    /**
     * 加载配置文件.
     * @param model
     * @throws Exception
     */
    private void initProperties(LicenseModel model) throws Exception {
        Properties prop = new Properties();
        prop.load(this.getClass().getResourceAsStream("/config/license/cp.properties"));
        String alias = prop.getProperty("ALIAS");
        String keypass = prop.getProperty("KEYPWD");
        String alg = prop.getProperty("KEYALG");
        String stroepass = prop.getProperty("STOREPWD");
        String licpath = prop.getProperty("LICPATH");
        String pripath = prop.getProperty("PRIPATH");

        model.setAlias(alias);
        model.setKeyAlg(alg);
        model.setKeyPass(keypass);
        model.setLicPath(licpath);
        model.setPriPath(pripath);
        model.setStorePass(stroepass);


        logger.info("init cp properties success");
    }

    /**
     * 初始化licenseModel.
     * @param license
     * @param model
     */
    public void initModel(License license, JiCengLicenseModel model) throws CheckParamException {
        if (null == license) {
            throw new CheckParamException("license is null");
        }

        if (null == license.getLicenseTemplate()){
            throw new CheckParamException("license.getLicenseTemplate() is null ");
        }

        model.setSubject(license.getLicenseMain().getSubject());
        model.setEdition(license.getLicenseTemplate().getType());
        model.setNotAfter(DateFormatUtils.format(license.getNotAfter(), "yyyy-MM-dd"));
        model.setNotBefore(DateFormatUtils.format(license.getNotBefore(), "yyyy-MM-dd"));
        model.setInfo(license.getInfo());
        BaseComputerModel computerModel = new BaseComputerModel();
        computerModel.setCPUSerial(license.getCpu());
        computerModel.setHardDiskSN(license.getDisk());
        computerModel.setIpAddress(license.getIp());
        computerModel.setIpMacAddress(license.getMac());
        computerModel.setMotherboardSN(license.getMotherboard());
        computerModel.setSystemSN(license.getSystem());
        model.setBaseComputerModel(computerModel);
        model.setMacheinesNums(license.getMachine());

        logger.info("init model success");
    }

    /**
     * 加载配置文件.
     * @param model
     * @throws Exception
     */
    private void initVpProperties(VerifyModel model) throws Exception {
        Properties prop = new Properties();
        prop.load(this.getClass().getResourceAsStream("/config/license/vp.properties"));

        String stroepass = prop.getProperty("STOREPWD");
        String pripath = prop.getProperty("PRIPATH");
        String alg = prop.getProperty("KEYALG");

        model.setKeyAlg(alg);
        model.setStorePass(stroepass);
        model.setPriPath(pripath);

        logger.info("init cp properties success");
    }

    /**
     * 初始化vmodel.
     * @param vmodel
     * @param model
     * @throws CheckParamException
     */
    private void initVmodel(VerifyModel vmodel, VerifyLicenseModel model) throws CheckParamException {
        if (null == model) {
            throw new CheckParamException("VerifyLicenseModel is null");
        }

        vmodel.setLicPath(model.getPath());
        vmodel.setEdition(model.getEdition());
        vmodel.setSubject(model.getSubject());

        if (LicenseConstant.NORAML_EDITION.equals(vmodel.getEdition())) {
            vmodel.setAlias("feilong");
        } else if (LicenseConstant.ENTERPRISE_EDITION.equals(vmodel.getEdition())) {
            vmodel.setAlias("feilong-qiye");
        }

        logger.info("init vmodel success");
    }
}
